package com.servlet.checking;

import com.domain.Checking;
import com.service.CheckingService;
import com.service.impl.CheckingServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/checkingAddServlet")
public class CheckingAddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        Checking checking = new Checking();
        try {
            BeanUtils.populate(checking,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        CheckingService ckservice = new CheckingServiceImpl();
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        if(ckservice.save(checking)){
            response.getWriter().write("{\"status\":\"0\",\"message\":\"ok\"}");
        }else{
            response.getWriter().write("{\"status\":\"1\",\"message\":\"no\"}");
        }

    }
}
